
package com.oahcfly.chgame.plist;

import java.util.HashMap;
import java.util.HashSet;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import com.oahcfly.chgame.core.mvc.CHGame;

/** Plist解析
 * 
 * @author haocao */
public class CHPListCenter {

	private static CHPListCenter instance;

	public static CHPListCenter getInstance () {
		if (instance == null) {
			instance = new CHPListCenter();
		}
		return instance;
	}

	private HashMap<String, CHPList> chplitMap = new HashMap<String, CHPList>();

	private HashMap<String, AtlasRegion> cacheRegionMap = new HashMap<String, AtlasRegion>();

	public Image getImageFromPList (String plistPath, String imgName) {
		Image image = new Image(new SpriteDrawable(getSprite(plistPath, imgName)));
		image.setOrigin(image.getWidth() / 2, image.getHeight() / 2);
		return image;
	}

	public TextureRegion getTextureRegion (String plistPath, String imgName) {
		return getSprite(plistPath, imgName);
	}

	public HashSet<String> getPlistPngNameSet (String plistPath) {
		CHPList chpList = null;
		if (chplitMap.containsKey(plistPath)) {
			chpList = chplitMap.get(plistPath);
		} else {
			chpList = new CHPList(plistPath);
			chpList.parse();
			chplitMap.put(plistPath, chpList);
		}
		return chpList.getPngNameSet();
	}

	public Sprite getSprite (String plistPath, String imgName) {
		CHPList chpList = null;
		if (chplitMap.containsKey(plistPath)) {
			chpList = chplitMap.get(plistPath);
		} else {
			chpList = new CHPList(plistPath);
			chpList.parse();
			chplitMap.put(plistPath, chpList);
		}

		String imgPath = plistPath.replace(".plist", ".png");
		FrameParam frameParam = chpList.getFrameParam(imgName);
		Texture texture = CHGame.getInstance().getTexture(imgPath);

		int w = frameParam.isRotated() ? frameParam.getHeight() : frameParam.getWidth();
		int h = frameParam.isRotated() ? frameParam.getWidth() : frameParam.getHeight();
		AtlasRegion textureRegion = null;
		if (!cacheRegionMap.containsKey(imgName)) {
			textureRegion = new TextureAtlas.AtlasRegion(texture, frameParam.getFromX(), frameParam.getFromY(), w, h);
			textureRegion.name = imgName;
			textureRegion.rotate = frameParam.isRotated();
			textureRegion.flip(frameParam.isRotated(), frameParam.isRotated());
			cacheRegionMap.put(imgName, textureRegion);
		} else {
			textureRegion = cacheRegionMap.get(imgName);
		}

		Sprite sprite = createSprite(textureRegion);
		return sprite;
	}

	public Sprite createSprite (AtlasRegion region) {
		if (region.rotate) {
			Sprite sprite = new Sprite(region);
			sprite.setBounds(0, 0, region.getRegionHeight(), region.getRegionWidth());
			sprite.rotate90(true);
			return sprite;
		}
		return new Sprite(region);
	}
}
